package com.bolehui.web;

import cn.hutool.core.util.ObjectUtil;
import com.bolehui.base.result.Result;
import com.bolehui.base.utils.SecurityUtil;
import com.bolehui.domain.dto.UserDTO;
import com.bolehui.domain.po.UserPO;
import com.bolehui.domain.po.iSys.SysMenu;
import com.bolehui.domain.vo.UserVO;
import com.bolehui.service.UserService;
import com.bolehui.service.iSys.ISysMenuService;
import com.bolehui.service.impl.PermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @author chendongjie     email:chendongjie@gz.iscas.ac.cn
 * @version 1.0
 * @ClassName UserController
 * @Description
 * @createTime 2024/12/20 14:37
 * Copyright (C) 2021-2022 CASEEDER, All Rights Reserved.
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 */

@RestController
@RequestMapping("/user.do")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询用户信息
     * @param userId
     * @return
     */
    @RequestMapping(params = "action=getUserInfo")
    public Result<UserVO> getUserPO(Long userId){
        if(userId == null){
            userId = SecurityUtil.getCurrentUserId();
        }
        return userService.getUserPO(userId);
    }

    /**
     * 更新用户信息
     * @param vo
     * @return
     */
    @RequestMapping(params = "action=updateUserPO")
    public Result<UserVO> updateUserPO(@RequestBody UserVO vo, Long id){
        return userService.updateUserPO(vo, id);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping(params = "action=getInfo")
    public Result getInfo(){
        UserPO loginUser = SecurityUtil.getCurrentUserInfo();
        if(ObjectUtil.isNull(loginUser)){
            return Result.fail("获取用户账户异常", HttpStatus.UNAUTHORIZED);
        }
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(loginUser);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(loginUser);
        // 更新用户权限信息
        // if (!loginUser.getPermissions().equals(permissions))
        // {
        //     loginUser.setPermissions(permissions);
        //     tokenService.refreshToken(loginUser);
        // }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(loginUser, userVO);
        userVO.setRoles(roles);
        userVO.setPermissions(permissions);
        return Result.success(userVO);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping(params = "action=getRouters")
    public Result getRouters()
    {
        Long userId = SecurityUtil.getCurrentUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return Result.success(menuService.buildMenus(menus));
    }

    @RequestMapping(params = "action=listUser")
    public Result userWithPage(Integer pageNum, Integer pageSize){
       return Result.success(userService.listUserWithPage(pageNum, pageSize));
    }

    @RequestMapping(params = "action=open")
    public Result open(Long id){
        return Result.success(userService.open(id));
    }

    @RequestMapping(params = "action=delete")
    public Result delete(Long id){
        return Result.success(userService.removeById(id));
    }

    /**
     * 注销
     */
    @RequestMapping(params = "action=logout")
    public Result logout(){
        return userService.logout();
    }
}


